package hk.ust.android_competition.activities;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import hk.ust.android_competition.e_pet.R;


public class CharacterSelectActivity extends LandscapeFullScreenActivity implements View.OnClickListener {
    private ImageButton imageButtonPanda;
    private ImageButton imageButtonRabbit;
    private ImageButton imageButtonGiraffe;

    private enum CharacterSelectActivityState {
        PetChoose, PetNameInput, UserNameInput;
    }

    CharacterSelectActivityState state;

    private EditText nameInput;

    int petIdChose = 0;
    private String petName = "";
    private String userName = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_character_select);
        imageButtonPanda = (ImageButton) findViewById(R.id.panda);
        imageButtonPanda.setOnClickListener(this);
        imageButtonRabbit = (ImageButton) findViewById(R.id.rabbit);
        imageButtonRabbit.setOnClickListener(this);
        imageButtonGiraffe = (ImageButton) findViewById(R.id.giraffe);
        imageButtonGiraffe.setOnClickListener(this);
        state = CharacterSelectActivityState.PetChoose;
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.panda:
                petIdChose = 0;
                state = CharacterSelectActivityState.PetNameInput;
                askName("What is my name: ", R.drawable.panda_normal);
                break;
            case R.id.rabbit:
                petIdChose = 1;
                state = CharacterSelectActivityState.PetNameInput;
                askName("What is my name: ", R.drawable.rabbit_normal);
                break;
            case R.id.giraffe:
                petIdChose = 2;
                state = CharacterSelectActivityState.PetNameInput;
                askName("Pet name: ", R.drawable.giraffe_normal);
                break;
            case R.id.name_input_submit:
                if (nameInput.getText().toString().matches(""))
                    Toast.makeText(getApplicationContext(), "You cannot leave it empty.", Toast.LENGTH_SHORT);
                else if (state == CharacterSelectActivityState.PetNameInput) {
                    petName = nameInput.getText().toString();
                    askName("Please enter your name: ", R.drawable.ask_name);
                    state = CharacterSelectActivityState.UserNameInput;
                } else if (state == CharacterSelectActivityState.UserNameInput) {
                    userName = nameInput.getText().toString();
                    Bundle extras = getIntent().getExtras();
                    Intent intent = new Intent();
                    intent.setClass(this, MainActivity.class);
                    intent.putExtra("data path", extras.getString("data path"));
                    intent.putExtra("status", "new user");
                    intent.putExtra("pet name", petName);
                    intent.putExtra("user name", userName);
                    intent.putExtra("pet type", petIdChose);
                    startActivity(intent);
                    finish();
                }
            default:
                break;
        }
    }

    private void askName(String display, int imageSourceId) {
        setContentView(R.layout.name_input);
        TextView displayText = (TextView) findViewById(R.id.displayText);
        displayText.setText(display);
        ImageView iv = (ImageView) findViewById(R.id.iv_name_input);
        iv.setImageResource(imageSourceId);
        Button submit = (Button) findViewById(R.id.name_input_submit);
        submit.setOnClickListener(this);
        nameInput = (EditText) findViewById(R.id.name_input);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        switch (state) {
            case PetChoose:
                finish();
                break;
            case PetNameInput:
                state = CharacterSelectActivityState.PetChoose;
                break;
            case UserNameInput:
                state = CharacterSelectActivityState.PetNameInput;
                break;
        }
    }
}
